package com.topView2;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: huangzhigao
 * @Date: 2023/2/12 15:49
 */
public class leecode46 {
    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {
        boolean[] visited = new boolean[nums.length];
        backtraverse(nums, 0, visited, new ArrayList<>());
        return res;
    }

    public void backtraverse(int[] nums, int depth, boolean[] visited, List<Integer> answer) {
        if (depth == nums.length) {
            res.add(new ArrayList<>(answer));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (!visited[i]) {
                visited[i] = true;
                answer.add(nums[i]);
                backtraverse(nums, depth + 1, visited, answer);
                visited[i] = false;
                answer.remove(answer.size() - 1);
            }

        }
    }
}
